// pages/coupon-center/index.js
import config from '../../etc/config'
const {
  $Toast
} = require('../../components/dist/base/index');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    toView: 'red',
    scrollTop: 100,
    shopId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.shopId) {
      this.data.shopId = options.shopId
    }
    this.getCouponList()
    let that = this;
    wx.getSystemInfo({
      success: function(res) {
        let clientHeight = res.windowHeight;
        let clientWidth = res.windowWidth;
        let ratio = 750 / clientWidth;
        let height = clientHeight * ratio;
        that.setData({
          height: height - 542
        });
      }
    })
  },
  upper(e) {
    // this.data.current = 1
    // this.data.couponList = []
    // this.getCouponList()
  },
  lower(e) {
    this.data.current += 1
    this.getCouponList()
  },
  scroll(e) {
    // console.log(e)
  },

  // 获取所有优惠券
  getCouponList() {
    let params = {
      userId: wx.getStorageSync('userId'),
      current: this.data.current,
      limit: 10
    }
    if (this.data.shopId) {
      params.shopId = this.data.shopId
    }
    let token = wx.getStorageSync('token')
    config._post('/coupon/couponCenter', token, params).then(res => {
      if (this.data.current === 1) {
        this.data.couponList = []
      }
      if (res.data.records && res.data.records.length > 0) {
        for (let i = 0; i < res.data.records.length; i++) {
          this.data.couponList.push(res.data.records[i])
        }
        this.setData({
          couponList: this.data.couponList,
        });
      }
    })
  },

  // 领取优惠券
  receive(e) {
    let params = {
      userId: wx.getStorageSync('userId'),
      couponId: e.target.dataset.couponid
    }
    let token = wx.getStorageSync('token')
    config._post('/coupon/getCouponByUser', token, params).then(res => {
      if (res.code == 0) {
        $Toast({
          content: '领取成功，请在我的优惠券中查看'
        })
        let coupon = 'couponList[' + e.target.dataset.index + '].isGet'
        this.setData({
          [coupon]: true
        })
      } else {
        $Toast({
          content: res.data.msg
        })
      }
      // this.setData({
      //   current: 1,
      // })
      // this.getCouponList()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    // this.getCouponList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      // title: wx.getStorageSync('mallName') + '——' + app.globalData.shareProfile,
      title: app.globalData.shareTitle,
      path: '/pages/coupon-center/index',
      imageUrl: app.globalData.shareImg,
      success: function(res) {
        // 转发成功
      },
      fail: function(res) {
        // 转发失败
      }
    }
  }
})